/*
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
解题思路：双指针，当左指针小于右指针时，循环继续。找到两边不符合要求的数，再交换位置。
 */
public class Offer21 {

  public static void main(String[] args) {
    int[] num = new int[]{1,2,3,4};
    int[] exchange = new Offer21().exchange(num);
    for (int i : exchange) {
      System.out.print(i + ",");
    }
  }
  public int[] exchange(int[] nums) {
    int left = 0;
    int right = nums.length - 1;
    while (left < right){
      while (left < right && (nums[left] % 2 != 0)) left++;
      while (left < right && (nums[right] % 2 == 0)) right--;
      int temp = nums[left];
      nums[left] = nums[right];
      nums[right] = temp;
    }
    return nums;
  }
}
